#ifndef __JSONTOOL__
#define __JSONTOOL__
#include <iostream>
#include <string>
#include <sstream>
#include <memory>
#include <jsoncpp/json/json.h>
#include"../Common/Log.hpp"

namespace JSONTOOL
{
    using namespace ns_log;
    class JsonTool
    {
    public:
        //序列化
        static bool Serialization(Json::Value& value,std::string* jsonstr)
        {
            Json::StreamWriterBuilder build;
            build["emitUTF8"] = true;
            std::stringstream ss;
            std::unique_ptr<Json::StreamWriter> swt(build.newStreamWriter());
            int ret = swt->write(value,&ss);
            if(ret < 0)
            {
                LOG(ERROR,"Serialization failed!\n");
                return false;
            }
            *jsonstr = ss.str();
            return true;
        }
        //反序列化
        static bool DelSerialization(Json::Value* value,const std::string& jsonstr)
        {
            Json::CharReaderBuilder build;
            build["emitUTF8"] = true;
            std::unique_ptr<Json::CharReader> srt(build.newCharReader());
            std::string errstr;
            if(srt->parse(jsonstr.c_str(),jsonstr.c_str() + jsonstr.size(),value,&errstr) == false)
            {
                LOG(ERROR,"反序列化失败！异常信息：= %s\n",errstr.c_str());
                return false;
            }
            return true;
        }
    };
}
#endif